package com.simtop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.logpo.Operate;
import com.simtop.service.OperateService;
import com.simtop.util.excelutil.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: liuzhaobo
 * @Description: 日志记录
 * @DateTime: 2022/8/31 0031 16:54
 * @Params:
 * @Return
 */
@CrossOrigin
@Controller
@RequestMapping("/operate")
public class OperateController {

    @Autowired
    private OperateService operateService;

    @Autowired
    private ExcelUtils excelUtils;

    /**
     * @Author: liuzhaobo
     * @Description: 获取日志信息
     * @DateTime: 2022/10/12 0012 11:59
     * @Params:
     * @Return
     */
    @RequestMapping("/getOperate")
    @ResponseBody
    public ServerResponse<Object> getOperate(@RequestBody Operate operate) {
        try {
            PageHelper.startPage(operate.getPageNum(), operate.getPageSize());
            List<Operate> operates = operateService.getOperate(operate);
            PageInfo<Operate> pageInfo = new PageInfo<Operate>(operates);
            return ServerResponse.createBySuccess("获取成功", pageInfo);
        } catch (Exception e) {
            return ServerResponse.createByErrorMsg("获取失败");
        }
    }

    /**
     * @Author: liuzhaobo
     * @Description: 导出日志信息
     * @DateTime: 2022/10/12 0012 12:18
     * @Params:
     * @Return
     */
    @GetMapping("/exportOperate")
    public void exportOperate(HttpServletRequest request, HttpServletResponse response) {
        List<Operate> operates = operateService.getOperate(null);
        excelUtils.cooperationData(request, response, operates, "日志文件", Operate.class);
    }
}
